package top.skihub.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import top.skihub.pojo.Goods;
import top.skihub.pojo.Result;
import top.skihub.service.SearchGoodsService;
import java.util.List;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Base64;

@Slf4j
@RestController
public class SearchGoodsController {
    @Value("${server.port}")
    private String port;

    @Autowired
    private SearchGoodsService searchGoodsService;

    @GetMapping("/searchallgoods")
    public Result list(){
        log.info("服务端口: {}, 开始调用加载全部商品接口", port);
        try {
            log.info("加载全部商品！");
            List<Goods> goodsList1 = searchGoodsService.list1();

            // 为每个商品添加图片Base64编码
            for (Goods goods : goodsList1) {
                if (goods.getGoodsImg() != null) {
                    String imagePath = "/usr/local/javaproject/supermarket-management-system" + goods.getGoodsImg();

                    try {
                        byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
                        String base64Image = Base64.getEncoder().encodeToString(imageBytes);
                        goods.setGoodsImgBase64("data:image/png;base64," + base64Image);
                    } catch (Exception e) {
                        log.error("读取商品图片失败: " + imagePath, e);
                        // 如果图片读取失败，设置一个默认的图片或者清空图片
                        goods.setGoodsImgBase64(null);
                    }
                }
            }

            log.info("服务端口: {}, 加载全部商品成功，商品数量: {}", port, goodsList1.size());
            return Result.success(goodsList1);
        } catch (Exception e) {
            log.error("加载全部商品失败！", e);
            return Result.error("加载全部商品失败！");
        }
    }

    @GetMapping("/searchgoods/{goodsName}")
    public Result list(@PathVariable String goodsName){
        log.info("服务端口: {}, 开始调用搜索商品接口，搜索商品名称: {}", port, goodsName);
        try {
            log.info("服务端口: {}, 开始搜索商品: {}", port, goodsName);
            List<Goods> goodsList2 = searchGoodsService.list2(goodsName);

            // 为搜索结果也添加图片处理
            for (Goods goods : goodsList2) {
                if (goods.getGoodsImg() != null) {
                    String imagePath = "/usr/local/javaproject/supermarket-management-system" + goods.getGoodsImg();

                    try {
                        byte[] imageBytes = Files.readAllBytes(Paths.get(imagePath));
                        String base64Image = Base64.getEncoder().encodeToString(imageBytes);
                        goods.setGoodsImgBase64("data:image/png;base64," + base64Image);
                    } catch (Exception e) {
                        log.error("读取商品图片失败: " + imagePath, e);
                        goods.setGoodsImgBase64(null);
                    }
                }
            }

            log.info("服务端口: {}, 搜索商品成功，商品数量: {}", port, goodsList2.size());
            return Result.success(goodsList2);
        } catch (Exception e) {
            log.error("搜索商品失败！", e);
            return Result.error("搜索商品失败！");
        }
    }
}